//package com.wsoft.bpm.service;
//
//import org.activiti.bpmn.model.BpmnModel;
//import org.activiti.bpmn.model.Process;
//import org.activiti.engine.RepositoryService;
//import org.activiti.engine.impl.persistence.deploy.DeploymentManager;
//import org.activiti.engine.impl.persistence.deploy.ProcessDefinitionCacheEntry;
//import org.activiti.engine.repository.Deployment;
//import org.activiti.engine.repository.DeploymentBuilder;
//import org.activiti.engine.repository.ProcessDefinition;
//import org.activiti.spring.SpringProcessEngineConfiguration;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.io.ByteArrayInputStream;
//
///**
// * @Author zhonglj
// * @Date 2024-11-07 16:53
// */
//@Service
//public class ProcessCacheService {
//    @Resource
//    private SpringProcessEngineConfiguration processEngineConfiguration;
//    @Resource
//    private RepositoryService repositoryService;
//    @Resource
//    private DeploymentManager deploymentManager;
//
//    public void updateProcessDefinition(String processDefinitionId, Process newProcess) {
//        // 获取当前的BpmnModel
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
//
//        if (bpmnModel != null) {
//            // 更新BpmnModel中的Process对象
//            bpmnModel.getProcesses().clear();
//            bpmnModel.getProcesses().add(newProcess);
//
//            // 更新缓存
//            ProcessDefinitionCacheEntry cacheEntry = deploymentManager.getProcessDefinitionCache().get(processDefinitionId);
//            if (cacheEntry != null) {
//                cacheEntry.setBpmnModel(bpmnModel);
//            }
//
//            // 更新数据库中的BpmnModel
//            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                    .processDefinitionId(processDefinitionId)
//                    .singleResult();
//
//            if (processDefinition != null) {
////                repositoryService.saveModel();
//            }
//        }
//    }
//
//
////    @Transactional
////    @Override
////    public int updateProcess(BpmnModel model, String processId) throws XMLStreamException {
////        // 获取模型流程图
////        byte[] bpmnBytes = model.getBpmn().getBytes();
////        // 获取模型流程图svg
////        byte[] svgBytes = model.getSvg().getBytes();
////        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
////        deploymentBuilder.addInputStream("diagram.bpmn", new ByteArrayInputStream(bpmnBytes));
////        deploymentBuilder.addInputStream("diagram.png",new ByteArrayInputStream(svgBytes));
////
////        // 流程分类
////        deploymentBuilder.category(model.getCategory());
////        // 流程名
////        deploymentBuilder.name(model.getName());
////        // 部署修改后的流程
////        Deployment deployment = deploymentBuilder.deploy();
////
////        // 修改activiti中流程图
////        // 查询原流程定义信息
////        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processId).singleResult();
////        // 删除新流程的部署记录
////        String deleteNewProcessSQL = "DELETE FROM ACT_RE_PROCDEF WHERE DEPLOYMENT_ID_ = '" + deployment.getId() + "'";
////        commonMapper.executeDeleteSql(deleteNewProcessSQL);
////        // 删除原流程图信息
////        String deleteOldProcessDataSQL = "DELETE FROM ACT_GE_BYTEARRAY WHERE DEPLOYMENT_ID_ = '" + processDefinition.getDeploymentId() + "'";
////        commonMapper.executeDeleteSql(deleteOldProcessDataSQL);
////
////        // 修改新部署的流程图文件的部署id为原流程的部署id
////        String updateNewProcessDataToOldProcessSQL =
////                "UPDATE ACT_GE_BYTEARRAY SET DEPLOYMENT_ID_ = '" + processDefinition.getDeploymentId() + "' " +
////                        "WHERE DEPLOYMENT_ID_ = '" + deployment.getId() + "'";
////        commonMapper.executeUpdateSql(updateNewProcessDataToOldProcessSQL);
////
////        String deleteDeploymentSQL = "DELETE FROM ACT_RE_DEPLOYMENT WHERE ID_ = '" + deployment.getId() + "'";
////        // 删除部署表记录
////        commonMapper.executeDeleteSql(deleteDeploymentSQL);
////        return 1;
////    }
//}
